package utils

import (
	"crypto/rand"
	"encoding/hex"
	"fmt"
	"math"
	"math/big"
)

// GenerateHex 生成指定长度的随机十六进制字符串
func GenerateHex(length int) string {
	bytes := make([]byte, length)
	rand.Read(bytes)
	return hex.EncodeToString(bytes)
}

// GenerateVerifyCode 生成6位数字验证码
func GenerateVerifyCode() string {
	bytes := make([]byte, 3)
	rand.Read(bytes)
	code := hex.EncodeToString(bytes)
	return code[:6]
}

func GenerateNumber(length int) string {
	if length <= 0 || length > 6 {
		panic("length must be between 1 and 6")
	}
	max := big.NewInt(9 * int64(math.Pow10(length-1)))
	min := big.NewInt(1 * int64(math.Pow10(length-1)))
	code, _ := rand.Int(rand.Reader, max.Sub(max, min))
	return fmt.Sprintf("%06d", code.Int64()+min.Int64())
}
